package com.wsw.common.util;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * pinyin4j extUtil
 * @author sw.wang
 *
 */
public class PinYinUtil {
	public static void main(String[] args)
			throws BadHanyuPinyinOutputFormatCombination {
		System.out.println(findPinYinSzm("湖北天元工程项目管理有限公司"));
	}
	
	/**
	 * 汉字转拼音(全拼)
	 * @param value 汉字
	 * @param seperater 分隔符
	 * @return
	 * @throws BadHanyuPinyinOutputFormatCombination
	 */
	@SuppressWarnings("deprecation")
	public static String findPinYin(String value,String seperater) throws BadHanyuPinyinOutputFormatCombination{
		HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
		defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
		return PinyinHelper.toHanyuPinyinString(value, defaultFormat, seperater);
	}
	
	/**
	 * 汉字转拼音(全拼)
	 * @param value 汉字
	 * @return
	 * @throws BadHanyuPinyinOutputFormatCombination
	 */
	@SuppressWarnings("deprecation")
	public static String findPinYin(String value) throws BadHanyuPinyinOutputFormatCombination{
		HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
		defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
		return PinyinHelper.toHanyuPinyinString(value, defaultFormat, "");
	}
	
	/**
	 * 汉字转拼音(首字母)
	 * @param value 汉字
	 * @return
	 * @throws BadHanyuPinyinOutputFormatCombination
	 */
	public static String findPinYinSzm(String value) throws BadHanyuPinyinOutputFormatCombination{
		char[] ch = value.toCharArray();
		String returnVal = "";
		for (int i = 0; i < ch.length; i++) {
			returnVal += PinyinHelper.toHanyuPinyinStringArray(ch[i])[0].charAt(0);
		}
		return returnVal;
	}
}
